package weaver.interfaces.jjy.duss.action;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.jjy.duss.hr.HMDSEL;
import weaver.interfaces.jjy.duss.hr.HRSEL;
import weaver.interfaces.jjy.duss.util.SAPComMehtod;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: ecology-9-demo
 * @description: 人员入职信息校验
 * @author: Dusongsong
 * @create: 2019-12-07
 */
public class WeaHRxxjyAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        BaseBean baseBean = new BaseBean();
        String requestid = requestInfo.getRequestid();
        baseBean.writeLog("请求ID" + requestid);
        baseBean.writeLog("调用WeaHRxxjyAction");
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String workflowid = requestInfo.getWorkflowid();
        String requestname = requestInfo.getRequestManager().getRequestname();
        User user = requestInfo.getRequestManager().getUser();
        int userid = user.getUID();
        int nodeid = requestInfo.getRequestManager().getNodeid();
        int modeid = Util.getIntValue(baseBean.getPropValue("WStablename", "logmodeid"));
        rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
        rs.next();
        String mainid = Util.null2String(rs.getString("id"));
        String sfzh = Util.null2String(rs.getString("sfzh"));//身份证
        if (sfzh.substring(sfzh.length() - 1).equalsIgnoreCase("x")) {
            sfzh = sfzh.replaceAll(sfzh.substring(sfzh.length() - 1), sfzh.substring(sfzh.length() - 1).toUpperCase());
        }
        baseBean.writeLog("身份证号：" + sfzh);
        Map resultMap = new HashMap();
        resultMap.put("ICNUM", sfzh);
        HRSEL hrsel = new HRSEL();
        if (sfzh.length() == 15 || sfzh.length() == 18) {
            baseBean.writeLog("身份证号输入正确");
            Map<String, String> returnMap = hrsel.execute(resultMap);
            String flag = "";
            String mesg = "";
            String sfyzxx = "";
            String xb = SAPComMehtod.getSex(sfzh);
            String csrq = SAPComMehtod.getBirthday(sfzh);
            String ygh = "";
            String xm = "";
            String jg = "";
            String ksrq3 = "";
            String jsrq3 = "";
            String zw = "";
            String jllx = "";
            String gzdw = "";
            String massn = "";
            String rybcz = "00000000";
            int sfyzjg = 0;
            for (String key : returnMap.keySet()) {
                ygh = returnMap.get("ygh");
                xm = returnMap.get("xm");
                //xb = returnMap.get("xb");
                jg = returnMap.get("jg");
                //csrq = returnMap.get("csrq");
                ksrq3 = returnMap.get("ksrq3");
                jsrq3 = returnMap.get("jsrq3");
                zw = returnMap.get("zw");
                jllx = returnMap.get("jllx");
                gzdw = returnMap.get("gzdw");
                massn = returnMap.get("massn");
            }
            if (!ygh.equals(rybcz)) {//人员存在
                HMDSEL hmdsel = new HMDSEL();
                Map<String, String> hmdMap = hmdsel.execute(resultMap);
                String flags = hmdMap.get("flag");
                String message = hmdMap.get("message");
                baseBean.writeLog("message="+message);
                if (flags.equalsIgnoreCase("E")) {//人员在黑名单中
                    sfyzjg = 3;//在黑名单中
                    sfyzxx = message;
                    rs.executeUpdate("update " + tablename + " set sfyzxx='" + sfyzxx + "', sfyzjg='" + sfyzjg + "' where requestid=?", requestid);
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                } else {
                    if (massn.equals("Z8")) {//人员离职
                        sfyzxx = "人员离职";
                        sfyzjg = 1;//离职返聘
                        rs.executeUpdate("update " + tablename + " set  sfyzjg= '" + sfyzjg + "' , ygh= '" + ygh
                                + "' , xm='" + xm + "' , xb='" + xb + "', jg='" + jg + "' , csrq='" + csrq + "', sfyzxx='" + sfyzxx
                                + "'  where requestid=?", requestid);

                        rs.executeUpdate("update " + tablename + "_dt2 set ksrq3='" + ksrq3 + "' , jsrq3='" + jsrq3 + "' , zw='" +
                                zw + "' , gzdw='" + gzdw + "' , jllx='" + jllx + "'  where mainid=?", mainid);
                    } else if (massn.equals("Z9")) {//人员退休
                        sfyzjg = 2;//退休返聘
                        sfyzxx = "人员退休";
                        rs.executeUpdate("update " + tablename + " set  sfyzjg= '" + sfyzjg + "' , ygh= '" + ygh
                                + "' , xm='" + xm + "' , xb='" + xb + "', jg='" + jg + "' , csrq='" + csrq + "', sfyzxx='" + sfyzxx
                                + "'  where requestid=?", requestid);

                        rs.executeUpdate("update " + tablename + "_dt2 set ksrq3='" + ksrq3 + "' , jsrq3='" + jsrq3 + "' , zw='" +
                                zw + "' , gzdw='" + gzdw + "' , jllx='" + jllx + "'  where mainid=?", mainid);

                    } else {
                        sfyzjg = 3;//异常
                        sfyzxx = "人员已存在(不为离职/退休人员)且不在S名单";
                        rs.executeUpdate("update " + tablename + " set sfyzxx='" + sfyzxx + "', sfyzjg='" + sfyzjg + "' where requestid=?", requestid);
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                    }
                }
            } else {//人员不存在，新员工入职
                sfyzjg = 0;//新员工入职
                sfyzxx = "人员不存在，新员工入职";
                rs.executeUpdate("update " + tablename + " set sfyzxx='" + sfyzxx + "', sfyzjg='" + sfyzjg + "', xb= '" + xb + "' , csrq= '" + csrq + "'  where requestid=?", requestid);
                baseBean.writeLog("调用的SQL:" + "update " + tablename + " set sfyzxx='" + sfyzxx + "', sfyzjg='" + sfyzjg + "', xb= '" + xb + "' , csrq= '" + csrq + "'  where requestid=?", requestid);
            }
        } else {
            requestInfo.getRequestManager().setMessageid("111100");
            requestInfo.getRequestManager().setMessagecontent("身份证号码错误");
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;
    }

}
